//
//  DetailMainTableViewController.m
//  WGFGlobalHouses
//
//  Created by waiguofang on 15/6/15.
//  Copyright (c) 2015年 waiguofang. All rights reserved.
//

#import "DetailMainTableViewController.h"
#import "HouseTypeViewController.h"
#import "HouseDetailCell1.h"
#import "HouseDetailCell2.h"
#import "NearbyCell2.h"
#import "HouseDetailDataManage.h"
#import "HouseTypeData.h"
#import "UserModal.h"
#import "SingletonData.h"
#import "NodataCell.h"
#import "DistanceCell.h"
#import "FangYuanCell.h"
#import "FangXingCell.h"
#import "HouseIntroducedCell.h"
#import "disDescModel.h"
#import "NearbyCellData.h"
#import "TxtReadVc.h"

#import "config.h"
#import "DetailSubViewController.h"


@interface DetailMainTableViewController ()<UITableViewDataSource,UITableViewDelegate,UIScrollViewDelegate>
@property(nonatomic,strong)UITableView *tableView1;
@property(nonatomic,strong)UITableView *tableView2;
@property (strong, nonatomic) HouseDetailDataManage *dataManage;
@property (strong, nonatomic) HouseTypeData *model;
@property (strong, nonatomic) NodataCell *nodataCell;

@property (nonatomic,strong)UITableViewCell *fxcel,*fZcell;

@property(nonatomic,assign) NSInteger flag;//flag = 0 tableview1,falg = 2

@end

@implementation DetailMainTableViewController
@synthesize nodataCell;

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self setEdgesForExtendedLayout:UIRectEdgeNone];
    [self initTableView];
    [self freshData];
    
   

}

-(void)initTableView
{
    
//    [self.titleView setMianTitle:@"房屋详情"];
//    self.titleView.doneBtn.hidden = YES;
    self.flag = 1;
    self.dataManage = [[HouseDetailDataManage alloc]init];
    
    UITableView *tableView1 = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, UIScreenWidth, UIScreenHeight - 64)];
    tableView1.delegate = self;
    tableView1.dataSource = self;
    tableView1.separatorStyle = UITableViewCellSelectionStyleNone;
    [self.view addSubview:tableView1];
    self.tableView1 = tableView1;
    [self.tableView1 setBackgroundColor:UIColorFromRGB(0xf3f3f3)];
    
}

#pragma mark 获取数据
-(void)freshData
{
    
    [nodataCell setState:nodataCellStateLoading];
    
    __weak typeof(self) bself = self;
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        
        ResulteMod *mod = [self.dataManage refreshDataWithConsumerId:[NSString stringWithFormat:@"%ld",(long)[UserModal shareUserModel].userId] WithPremiseshousingId:self.premiseshousingId];
        
        dispatch_async(dispatch_get_main_queue(), ^{
            
            
            if (mod.resltState == ResultNetError) {//网络出错
                
                [bself.nodataCell setState:nodataCellStateNetError];
                
            }else if(mod.resltState == ResultFail){//服务器返回的code出错
                
                [bself.nodataCell setCodeErrorTip:mod.resultCode];
                
            }else{//返回正常
                
                self.model = mod.dataMod;
                
                if(self.model == nil){//没数据
                    
                    [bself.nodataCell setState:nodataCellStateNodata];
                }
            }
            
            [bself.tableView1 reloadData];
            DetailSubViewController *vc = [DetailSubViewController new];
            vc.model = self.model;
            self.subTableViewController = vc;
            
        });
    });
}



-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
   
    if (self.model!=nil)
    {
        return self.model.nearbyArr.count + 2 ;
        
    }else{
        return   1;
    }

}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (self.model == nil)
    {
        if(!nodataCell){
            nodataCell = [[NodataCell alloc] initWithFrame:self.tableView1.frame];
            [nodataCell setState:nodataCellStateLoading];
            [[nodataCell refreshBtn]setTag:56];
            [nodataCell.refreshBtn addTarget:self action:@selector(btnOnclick:) forControlEvents:UIControlEventTouchUpInside];
        }
        return nodataCell;
    }
    else
    {
        if (indexPath.row == 0)
            {
                HouseDetailCell1 *cell = [HouseDetailCell1 cellWithTableView:tableView data:self.model];
                cell.data1 = self.model;
                return cell;
            }else if (indexPath.row == self.model.nearbyArr.count + 1)
            {
                HouseDetailCell2 *cell = [HouseDetailCell2 cellWithTableView:tableView];
                cell.data = self.model;
                return cell;
            }else
            {
                NearbyCell2 *cell = [NearbyCell2 cellWithTableView:tableView withData:[self.model.nearbyArr objectAtIndex:indexPath.row - 1]];
                cell.data = [self.model.nearbyArr objectAtIndex:indexPath.row - 1];
                return cell;
            }
    }
    return nil;
    
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (self.model!=nil) {
        [tableView setScrollEnabled:YES];
            if (indexPath.row == 0)
            {
                return 586;
            }else if (indexPath.row == self.model.nearbyArr.count +1)
            {
                return 220;
            }else
            {
                return 50;
            }
    }

        [tableView setScrollEnabled:NO];
        return tableView.frame.size.height;

    
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (self.model != nil && self.flag == 1)
    {
        if (indexPath.row == 1)
        {
            NearbyCellData *data= [self.model.nearbyArr objectAtIndex:indexPath.row - 1];
            if (data.moreDetial != nil && indexPath.row == 1)
            {
                TxtReadVc *vc = [[TxtReadVc alloc]init];
                vc.titleStr = @"周边学校";
                vc.subCategoryTitleStr = data.detail;
                vc.txtStr = data.moreDetial;
                 vc.hidesBottomBarWhenPushed = YES;
                [self.navigationController pushViewController:vc animated:YES];
            }
            
        }
    }
    
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}
@end
